/**
 * 
 */
package com.java.controller.front;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author Kidney
* 创建时间：2019年2月9日 下午10:07:20
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
public class VerifyCodeController {

	 @RequestMapping("/VerifyCode")
	    public void VerifyCode(HttpServletRequest request, HttpServletResponse response) {

	        // 创建一个宽100,高50,且不带透明色的image对象 100 50
	        BufferedImage bi = new BufferedImage(260, 50, BufferedImage.TYPE_INT_RGB);
	        Graphics g = bi.getGraphics();
	        //RGB色彩
	        Color c = new Color(200, 150, 255);
	        // 框中的背景色
	        g.setColor(c);
	        // 颜色填充像素
	        g.fillRect(0, 0, 260, 50);

	        // 定义验证码字符数组
	        char[] ch = "ABCDEFGHKMNPQRSTUVWXYZ123456798".toCharArray();
	        Random r = new Random();
	        int len = ch.length;
	        int index;
	        StringBuffer sb = new StringBuffer();
	        // 取出四个数字
	        for (int i = 0; i < 4; i++) {
	            // 循环四次随机取长度定义为索引
	            index = r.nextInt(len);
	            g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt(255)));
	            Font font = new Font("Times New Roman", Font.ITALIC, 18);
	            g.setFont(font);
	            g.drawString(ch[index] + "", (i * 18) + 10, 30);
	            sb.append(ch[index]);
	        }
	        //放入Session中
	        request.getSession().setAttribute("piccode", sb.toString());
	        try {
	            ImageIO.write(bi, "JPG", response.getOutputStream());
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }

	
}
